package com.itany.corejava.code09_集合.Set;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author 石小俊
 * 2024年07月24日 14:52
 */
public class Test03_判断元素是否重复 {

    public static void main(String[] args) {
        Student student = new Student(1, "admin");
        Student student2 = new Student(1, "admin");
        Student student3 = new Student(3, "alice");
        System.out.println(student.hashCode());
        System.out.println(student2.hashCode());
        System.out.println(student3.hashCode());
        System.out.println(student);
        System.out.println(student2);
        System.out.println(student3);
        Set<Student> students = new HashSet<>();
        students.add(student);
        students.add(student2);
        students.add(student3);
        System.out.println(students);

    }

}

class Student {
    private int no;
    private String name;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }

    public Student() {
    }

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
